package org.flashbrighton.as3bots.utils
{
	import org.flashbrighton.as3bots.models.BotModel;

	public class MathUtils
	{
		public function MathUtils()
		{
		}

		private static const PI:Number= Math.PI;
		private static const TWOPI:Number = Math.PI*2;
		
		/**
		 * Return a number given the direction of the point  0 if in front of the bot, -1 or 1 if at the back...
		 */
		public static function getDirection(bot:BotModel, x:Number, y:Number):Number
		{
			var botAngle:Number = bot.body.GetAngle();			
			
			botAngle = normalizedAngle(botAngle)*PI;
			
			var dirAngle:Number = Math.atan2(y - bot.body.GetPosition().y, x - bot.body.GetPosition().x) - botAngle;
			var direction:Number = dirAngle / PI;
			if (direction > 1) direction -= 2;
			if (direction < -1) direction += 2;
			
			return direction;
		}

		/**
		 * return 1 if value is 0 or positive, -1 if negative;
		 */
		public static function sign(value:Number):Number
		{
			var result:int = 0;
			if(value>=0){
				result = 1;
			}else if (value<0){
				result = -1;
			}
			return result;
		}
		
		public static function normalizedAngle(angle:Number):Number
		{
			angle += PI;
			angle %= TWOPI;
			if (angle < 0) angle += TWOPI;
			angle -= PI;
			return angle/PI;
		}
	}
}